package com.woniuxy.classteacher.uitility;

import com.woniuxy.commons.exception.EmailLaunchException;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


/**
 * @Auther: 牟强
 * @Date: 2020/7/18/018 15:39
 * @Description:
 */
public class EmailUtil {
    public static void sendMail(String email) {
        try {
            final Properties props = new Properties();
            props.put("mail.user", "727344015@qq.com");
            props.put("mail.password", "nrewmphmurpfbegc"); // 授权码
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.ssl.enable", "true");//开启加密传输
            System.out.println("-------------发送前-------------------------------------");
            Session mailSession = Session.getDefaultInstance(props);

            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress("727344015@qq.com"));  //发件人
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); //收件人
            msg.setSubject("通知邮件-巴中普通高中"); //主题
            msg.setContent("<h1>此邮件为巴中普通高中通知邮件！</h1><br />" +
                    "<h4>密码已成功修改为默认密码：000000</h4>", "text/html;charset=UTF-8");
            msg.saveChanges();

            System.out.println("-------------发送后-------------------------------------");
            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.user"),
                    props.getProperty("mail.password"));
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            throw new EmailLaunchException("邮箱发送失败");
        }
    }
}